package ThreadDemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 34255
 * Date: 2024-10-16
 * Time: 14:06
 */
//锁的可重入
public class ReentrantLockDemo {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            synchronized (Object.class) {
                for (int i = 0; i < 50; i++) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //将Object类的字节码文件对象作为锁对象
                    synchronized (Object.class) {//同一个线程里，线程对Object.class引用进行了加锁
                        CountTest.setCount();//后面在对该锁对象进行了加锁操作
                    }
                }
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (Object.class){
                System.out.println("hello");
            }
        });

        thread.start();
        thread2.start();
        thread.join();
        thread2.join();
        System.out.println(CountTest.getCount());
    }
}
